package dst.ass1.jpa.model.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import dst.ass1.jpa.model.IExpert;
import dst.ass1.jpa.model.ITaskForce;


@NamedQueries({
	 //1.2.b
	 //find all experts with rst name starting with 'Alex' and, for each of them, calculate the next upcoming meeting 
	 //(get the earliest nextMeeting attribute among all the task forces of the respective expert)
	 //used in ExpertDAO
	  @NamedQuery(
	    name="taskforcesOfExpert",
	    query="SELECT DISTINCT e, min(t.nextMeeting) FROM Expert e " + //min nextMeeting for expert
	        "LEFT JOIN e.advisedTaskForces t " +
	        "WHERE e.firstName like 'Alex%' AND t.nextMeeting IS NOT null "  //name must begin with Alex and nextMeeting should not be null
	        + "GROUP BY e"   //group by expert and geht minimal nextMeeting
	  )
})
@Entity
public class Expert extends Person implements IExpert{

	private final static Logger LOGGER = Logger.getLogger(Expert.class.getName());
	
	@OneToMany(targetEntity=TaskForce.class, mappedBy="expert", fetch=FetchType.LAZY) //to-star(collection) props are loaded lazily (as the number of items may well be very large
	private List<ITaskForce> advisedTaskForces;
	
	public Expert (){
		advisedTaskForces = new ArrayList<ITaskForce>();
	}
	
	@Override
	public List<ITaskForce> getAdvisedTaskForces() {
		return this.advisedTaskForces;
	}

	@Override
	public void setAdvisedTaskForces(List<ITaskForce> taskForces) {
		this.advisedTaskForces=taskForces;
	}

	@Override
	public void addAdvisedTaskForce(ITaskForce taskForce) {
		if(advisedTaskForces!=null){
			advisedTaskForces.add(taskForce);
		}
		else{
			LOGGER.info("Expert.List<ITaskForce> was null");
		}
		
	}

}
